/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.user.service.impl;

import java.sql.Connection;
import java.util.List;

import net.comm.util.DatetimeUtil;
import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.common.common;

import net.comm.user.bean.BaseUserSet;
import net.comm.user.db.DbUserSetService;
import net.comm.user.db.UserSetServiceSqlMysql;
import net.comm.user.web.form.UserSetForm;
import net.comm.util.DbcpPool;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;


/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class UserSetServiceImpl {

	static Logger logger = Logger.getLogger(UserSetServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(UserSetForm theForm,  ReturnMessage msg) 
	{
		List list = null;
		
		return list;
	}

	/**
	 * 返回根据会员ID查询的会员操作日志
	 */
	public List list(String  userID) 
	{
		List list = null;
		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = userID;			

			UserSetServiceSqlMysql servSql = new UserSetServiceSqlMysql();
						
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getByUserIDSql();
									
			list = bSqlServ.dbRead(cn, sql, fields, new DbUserSetService());
									
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}
	
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(UserSetForm theForm, ReturnMessage msg) 
	{

		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			get(cn, theForm, msg);			

			DbcpPool.closeTran(cn);			
			DbcpPool.returnConnection(cn);			
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}	
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}


	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(Connection cn, UserSetForm theForm, ReturnMessage msg) 
	{

		try
		{
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getUserID();
						
			UserSetServiceSqlMysql servSql = new UserSetServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();
						
			List list = bSqlServ.dbRead(cn, sql, fields, new DbUserSetService());
			
			if(list.size() > 0){
				BaseUserSet base = (BaseUserSet)list.get(0);
				BeanUtils.copyProperties(theForm, base);
				
				return;				
			}
			//不存在 则新增一条
			DbcpPool.beginTran(cn);

			Object[] usersetfields = new Object[8];
			
			usersetfields[0] = theForm.getUserID();
			usersetfields[1] = GlbFinal.USERSET_MEMBER;
			usersetfields[2] = GlbFinal.USERSET_MEMBER;
			usersetfields[3] = GlbFinal.USERSET_MEMBER;
			usersetfields[4] = GlbFinal.USERSET_MEMBER;
			usersetfields[5] = GlbFinal.USERSET_MEMBER;
			usersetfields[6] = GlbFinal.USERSET_MEMBER;
			usersetfields[7] = DatetimeUtil.getDatetime();
			
			sql = servSql.getInsertSql();
			
			boolean res = bSqlServ.dbWrite(cn, sql, usersetfields);
			if(!res){
				common.setAppFailMsg(msg , " 更新记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}			
			DbcpPool.commitTran(cn);
		
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}	
		} 
		
		return ;
	}
	
	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(UserSetForm theForm, ReturnMessage msg) 
	{
		
		return ;
	}

	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(UserSetForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			//取原记录的信息			
			Object[] fields2 = new Object[1];

			fields2[0] = theForm.getUserID();
			
			List list = null;
			
			UserSetServiceSqlMysql servSql = new UserSetServiceSqlMysql();
			
			String sql = servSql.getbykeySql();
		
			BasicSqlService  bSqlServ = new BasicSqlService();
		
			list = bSqlServ.dbRead(cn, sql, fields2, new DbUserSetService());
			
			if(list.size() == 0){
				
				common.setAppFailMsg(msg , " 记录不存在，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			//填充到theForm的空字段中
			BaseUserSet base = (BaseUserSet)list.get(0);
						
			if(theForm.getPicDisflg().equals("")){
				
				theForm.setPicDisflg(base.getPicDisflg());
			}
			
			if(theForm.getFriendFlg().equals("")){
				
				theForm.setFriendFlg(base.getFriendFlg());
			}
			if(theForm.getFirendListFlg().equals("")){
				
				theForm.setFirendListFlg(base.getFirendListFlg());
			}
			if(theForm.getEmailFlg().equals("")){
				
				theForm.setEmailFlg(base.getEmailFlg());
			}
			if(theForm.getHomeFlg().equals("")){
				
				theForm.setHomeFlg(base.getHomeFlg());
			}
			if(theForm.getPubOperFlg().equals("")){
				
				theForm.setPubOperFlg(base.getPubOperFlg());
			}
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[8];
			
			fields[0] = theForm.getPicDisflg();
			fields[1] = theForm.getFriendFlg();
			fields[2] = theForm.getFirendListFlg();
			fields[3] = theForm.getEmailFlg();
			fields[4] = theForm.getHomeFlg();
			fields[5] = theForm.getPubOperFlg();
			fields[6] = DatetimeUtil.getDatetime();
			fields[7] = theForm.getUserID();
			
			sql = servSql.getUpdateSql();
			
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 更新记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);			
			DbcpPool.returnConnection(cn);
			
		} catch(Exception ex) 
		{
			logger.error("System error , param Update about userdetails" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}	
		} finally {
			DbcpPool.returnConnection(cn);
		}
				
		return ;
	}

}
